package com.company.controller;

import com.company.base.User;
import com.company.core.annotation.Login;
import com.company.core.result.Result;
import com.company.core.result.ResultUtil;
import com.company.core.validator.ValidatorUtils;
import com.company.core.validator.group.AddGroup;
import com.company.utils.AssertUtils;
import com.company.utils.Constant;
import com.company.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @program: planet-company
 * @description: 用户处理
 * @author: jyz
 * @create: 2019-04-01 17:19
 **/
@Api(value = "用户")
@RestController
@RequestMapping("api/user")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private RedisUtils R;

    /**
     * 手机号登录
     * @return
     */
    @PostMapping("/phoneLogin")
    @ApiOperation(value = "手机号登录")
    public Result phoneLogin(HttpServletRequest request, @ApiParam("BBUser") @RequestBody User BBUser){
        ValidatorUtils.validateEntity(BBUser,AddGroup.class);
        User user = R.get(Constant.CompanyUser+BBUser.getPhone(),User.class);
        AssertUtils.isNull(user,"该手机号不存在");
        AssertUtils.isFalse(user.getPhone().equals(BBUser.getPhone()),"手机号有误，请检查！");
        AssertUtils.isFalse(user.getPassword().equals(BBUser.getPassword()),"密码有误，请检查！");
        request.getSession().setAttribute("phone",BBUser.getPhone());
        //默认结果是成功的
        Result result = ResultUtil.success();
        return result;
    }

    /**
     * 退出登录
     * @return
     */
    @Login
    @GetMapping("/logout")
    @ApiOperation(value = "退出登录")
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("phone");
        return ResultUtil.success("已退出");
    }



    /**
     * 获取用户信息
     * @return
     */
    @Login
    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    public Result getUserInfo(HttpServletRequest request){
        String phone = (String) request.getSession().getAttribute("phone");
        User user = R.get(Constant.CompanyUser+phone,User.class);
        AssertUtils.isNull(user,"该用户不存在，请检查！");
        return ResultUtil.success(user);
    }


    /**
     * 添加账户信息
     * @return
     */
    @PostMapping("/addUser")
    @ApiOperation(value = "添加账户信息")
    public Result addUser(HttpServletRequest request, @ApiParam("User") @RequestBody User User){
        ValidatorUtils.validateEntity(User,AddGroup.class);
        AssertUtils.isNullOrEmpty(User.getCompanyName(),"companyName不能为空");
        AssertUtils.isNullOrEmpty(User.getOpenId(),"openId不能为空");
        R.set(Constant.CompanyUser+User.getPhone(),User);
        //默认结果是成功的
        Result result = ResultUtil.success();
        return result;
    }
}
